<?php
class Admin_KhachHangController extends Zend_Controller_Action
{

    public function init()
    {
       $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$khachhangMapper = Library_Model_KhachhangMapper::getInstance();
    	$khachhang = $khachhangMapper->khachhangList();
    	$this->view->khachhang = $khachhang;
    	
    }

    public function danhsachAction()
    {
    	$id = $this->_request->getParam('id');
    	$khachhangMapper = Library_Model_KhachHangMapper::getInstance();
    	$danhsach = $khachhangMapper->searchById($id);
    	$this->view->danhsach = $danhsach;
    }
    
    public function themAction()
    {
    	$this->view->title = 'Thêm khách hàng';
    	$formThem = new Admin_Form_KhachhangForm();
    	
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/khachhang/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	 /* $formThem->getElement('hovaten')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_khachhang',
    			'field' => 'HoVaTen'
    	));  */
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$hovaten 			= $data['hovaten'];
    			$ngaysinh		= $data['ngaysinh'];
    			$cmnd 				= $data['cmnd'];
    			$ngaycap			= $data['ngaycap'];
    			$noicap			= $data['noicap'];
				$diachithuongtru		= $data['diachithuongtru'];
				$diachilienlac			= $data['diachilienlac'];
				$dienthoai				= $data['dienthoai'];
				$tencoquan				= $data['tencoquan'];
				$dienthoaicoquan	=$data['dienthoaicoquan'];
				$diachicoquan			=$data['diachicoquan'];
				
				
    			$khachhangMapper = Library_Model_KhachhangMapper::getInstance();
    			$khachhangObj 	= new Library_Model_Khachhang();
    			    			
    			$khachhangObj->setHovaten($hovaten);
				$khachhangObj->setNgaysinh($ngaysinh);
				$khachhangObj->setCmnd($cmnd);
				$khachhangObj->setNgaycap($ngaycap);
				$khachhangObj->setNoicap($noicap);
				$khachhangObj->setDiachithuongtru($diachithuongtru);
				$khachhangObj->setDiachilienlac($diachilienlac);
				$khachhangObj->setDienthoai($dienthoai);
				$khachhangObj->setTencoquan($tencoquan);
				$khachhangObj->setDienthoaicoquan($dienthoaicoquan);
				$khachhangObj->setDiachicoquan($diachicoquan);
				
    			$khachhang = $khachhangMapper->save($khachhangObj);
    			$this->_redirect('/admin/khachhang/index');
    		}
    	}
    }

    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật khách hàng';
        $mathe = $this->getRequest()->getParam('id');
        $formCapnhat = new Admin_Form_KhachhangForm($mathe);
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/khachhang/capnhat/');
       
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $id = $this->getRequest()->getParam('id');
        $formCapnhat->addElement('hidden', 'id');
      
        $khachhangMapper = Library_Model_KhachhangMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$hovaten 			= $data['hovaten'];
    			$ngaysinh		= $data['ngaysinh'];
    			$cmnd 				= $data['cmnd'];
    			$ngaycap			= $data['ngaycap'];
    			$noicap			= $data['noicap'];
				$diachithuongtru		= $data['diachithuongtru'];
				$diachilienlac			= $data['diachilienlac'];
				$dienthoai				= $data['dienthoai'];
				$tencoquan				= $data['tencoquan'];
				$dienthoaicoquan	=$data['dienthoaicoquan'];
				$diachicoquan			=$data['diachicoquan'];
								
    			$khachhangObj 	= new Library_Model_Khachhang();

    			$khachhangObj->setId($id);
    			$khachhangObj->setHovaten($hovaten);
				$khachhangObj->setNgaysinh($ngaysinh);
				$khachhangObj->setCmnd($cmnd);
				$khachhangObj->setNgaycap($ngaycap);
				$khachhangObj->setNoicap($noicap);
				$khachhangObj->setDiachithuongtru($diachithuongtru);
				$khachhangObj->setDiachilienlac($diachilienlac);
				$khachhangObj->setDienthoai($dienthoai);
				$khachhangObj->setTencoquan($tencoquan);
				$khachhangObj->setDienthoaicoquan($dienthoaicoquan);
				$khachhangObj->setDiachicoquan($diachicoquan);
				
    			$khachhang = $khachhangMapper->save($khachhangObj);
    			$this->_redirect('/admin/khachhang/index');
        	}
        } else {
        		$khachhang = $khachhangMapper->searchById($id);
        		
        		$formCapnhat->getElement('id')->setValue($khachhang->getId());
        		$formCapnhat->getElement('hovaten')->setValue($khachhang->getHovaten());        		
        		$formCapnhat->getElement('ngaysinh')->setValue($khachhang->getNgaysinh());
        		$formCapnhat->getElement('cmnd')->setValue($khachhang->getCmnd());
        		$formCapnhat->getElement('ngaycap')->setValue($khachhang->getNgaycap());
				$formCapnhat->getElement('noicap')->setValue($khachhang->getNoicap());
				$formCapnhat->getElement('diachithuongtru')->setValue($khachhang->getDiachithuongtru());
				$formCapnhat->getElement('diachilienlac')->setValue($khachhang->getDiachilienlac());
				$formCapnhat->getElement('dienthoai')->setValue($khachhang->getDienthoai()); 
				$formCapnhat->getElement('tencoquan')->setValue($khachhang->getTencoquan());
				$formCapnhat->getElement('dienthoaicoquan')->setValue($khachhang->getDienthoaicoquan());
				$formCapnhat->getElement('diachicoquan')->setValue($khachhang->getDiachicoquan());
				
			}
        
    }

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $makhachhang = $this->getRequest()->getParam('id');
        $khachhangMapper = Library_Model_KhachhangMapper::getInstance();
        $khachhangMapper->delete($makhachhang);
        $this->_redirect('/admin/khachhang/index');
    }
    
    
   public function validateformAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_KhachHangForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }


}









